﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using WMSmanagement.IBaseService.Sale;
using WMSmanagement.IBaseService.Sale.DTO;

namespace WMSmanagement.Web.Controllers
{
    public class SalesController1 : Controller
    {
        private readonly ISaleService _saleService;

        public SalesController1(ISaleService saleService)
        {
            _saleService = saleService;
        }

        public IActionResult Show()
        {
            return View();
        }
        public IActionResult Add()
        {
            return View();
        }
        public IActionResult Update()
        {
            return View();
        }
        public async Task<JsonResult> GetSales(RequestSaleDTO model) 
        {
            var list =await _saleService.GetSaleAsync(model);
            return Json(list);
        }
        public async Task<JsonResult> GetProducts()
        {
            var list=await _saleService.GetProductsAsync();
            return Json(list);
        }
        public async Task<IActionResult> AddSale(AddSaleDTO model)
        {
            model.Ids=JsonConvert.DeserializeObject<List<Buyids>>(model.IdsString);
            var sum = 0;
            model.Ids.ForEach(c =>
            {
                c.Num=model.OutQuantity;
                
            });
            var result=await _saleService.AddSaleAsync(model);
            return Ok(result);
        }
        public async Task<IActionResult> DelSale(string id)
        {
            var result=await _saleService.DeleteSaleAsync(id);
            return Ok(result);
        }
        public async Task<IActionResult> RetSlae(int id)
        {
            var result=await _saleService.ReturnSaleAsync(id);
            return Ok(result);
        }
        public async Task<IActionResult> UpdateSale(UpdateSaleDTO model)
        {
            model.Ids = JsonConvert.DeserializeObject<List<Buyids>>(model.IdsString);
            var sum = 0;
            model.Ids.ForEach(c =>
            {
                c.Num = model.OutQuantity;

            });
            var result=await _saleService.UpdateSaleAsync(model);
            return Ok(result);
        }
    }
}
